package com.wanshi.dao;

import com.wanshi.entity.Sheng;
import com.wanshi.entity.Shi;
import com.wanshi.entity.Xian;
import com.wanshi.utils.JDBCUtil;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Random;

public class AreaDao {

    public static void main(String[] args) throws IOException {

        File fromPic=new File("D:/T164/pic/123.webp");
        File toPic=new File("D:/T164/pic/123_s.png");

        Thumbnails.of(fromPic).scale(0.2f).outputFormat("png").toFile(toPic);//按比例缩小
    }

    //1 获取所有省份
    public List<Sheng> getShengs() {
        QueryRunner runner = new QueryRunner(JDBCUtil.getSource());
        try {
            return runner.query("select * from provinces",new BeanListHandler<>(Sheng.class));
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("查询省份失败");
        }
    }


    //2 根据省的id获取所有的市
    public List<Shi> getShis(String id) {
        QueryRunner runner = new QueryRunner(JDBCUtil.getSource());
        try {
            return runner.query("select * from cities where provinceid = ?",
                    new BeanListHandler<>(Shi.class),id);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("查询市失败");
        }
    }

    //3 根据市的id获取所有的县
    public List<Xian> getXians(String id) {
        QueryRunner runner = new QueryRunner(JDBCUtil.getSource());
        try {
            return runner.query("select * from areas where cityid = ?",
                    new BeanListHandler<>(Xian.class),id);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("查询县失败");
        }
    }
}
